package com.zzh.structuredeployment.frag

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.zzh.custom.dialog.CommonDialog
import com.zzh.structuredeployment.R
import com.zzh.structuredeployment.adapter.FOOTER
import com.zzh.structuredeployment.adapter.HEADER
import com.zzh.structuredeployment.adapter.ITEM
import com.zzh.structuredeployment.adapter.ReleaseAdapter
import com.zzh.structuredeployment.bean.ReleaseBean
import com.zzh.tools.utils.ToolLog
import kotlinx.android.synthetic.main.fragment_release.*


/**
 * A simple [Fragment] subclass.
 * Use the [ReleaseFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class ReleaseFragment : Fragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_release, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    companion object {
        @JvmStatic
        fun newInstance() = ReleaseFragment()
    }

    fun initView() {
        configRecyclerView(creatInitialView())
    }

    private fun configRecyclerView(list: ArrayList<ReleaseBean>) {
        selectPicList.layoutManager = createLayoutManager()
        var adapter = createReleaseAdapter(list)
        selectPicList.adapter = adapter
        selectPicList.addItemDecoration(adapter.mItemDecoration)
        selectPicList.itemAnimator?.changeDuration = 0
        adapter.mItemTouchHelper.attachToRecyclerView(selectPicList)
    }

    private fun creatInitialView(): ArrayList<ReleaseBean> {
        var list = arrayListOf<ReleaseBean>()
        list.add(createHeader())
        list.add(createAddPic())
        list.add(createFooter())
        return list
    }

    private fun createLayoutManager(): GridLayoutManager {
        var layoutManager = GridLayoutManager(context, 4)
        layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                selectPicList.adapter?.let {
                    it as ReleaseAdapter
                    var viewType = it.beans[position].viewType
                    return when (viewType) {
                        HEADER -> layoutManager.spanCount
                        FOOTER -> layoutManager.spanCount
                        ITEM -> 1
                        else -> 1
                    }
                }
                return 1
            }
        }
        return layoutManager
    }

    private var mAddPosition = -1
    private fun createReleaseAdapter(beans: ArrayList<ReleaseBean>): ReleaseAdapter {
        var releaseAdapter = ReleaseAdapter(beans)
        releaseAdapter.mOnItemClickLesenter = object : ReleaseAdapter.OnItemClickListener {
            override fun onAddClick(position: Int) {
                mAddPosition = position
                var list = arrayListOf<String>()
                list.add("http://image.gongjiangren.net/portrait/202011/7cf76408efe14e523fe346212e732d59.jpg")
                addPic(list)
            }

            override fun onImgClick(position: Int) {
                var urlList = releaseAdapter?.beans!!
                ToolLog.i("zzh", "点击了item>>${position}")
            }

            override fun onRemove(position: Int) {
                CommonDialog.Builder()
                    .setLeft("取消")
                    .setRight("确认")
                    .setShowLeft(true)
                    .setShowRight(true)
                    .setTitle("确认删除该图片吗?")
                    .setShowContent(false)
                    .setShowTitle(true)
                    .setOnLeftClickListener {

                    }
                    .setOnRightClickListener {
                        (selectPicList.adapter as ReleaseAdapter).removeItem(position)
                    }
                    .create(this@ReleaseFragment).show("remove")
            }
        }
        releaseAdapter.mOnItemDeleteListener = object : ReleaseAdapter.OnItemDeleteListener() {

            override fun showDelete() {
                delete.visibility = View.VISIBLE
                delete.text = "拖动到此处删除"
            }

            override fun showDeletePrompt() {
                delete.text = "松手即可删除"
            }

            override fun hideDelete() {
                delete.visibility = View.GONE
            }
        }
        return releaseAdapter!!
    }

    private fun addPic(list: List<String>) {
        selectPicList.adapter?.let {
            it as ReleaseAdapter
            var beans = arrayListOf<ReleaseBean>()
            list.forEach { path ->
                beans.add(createPicBean(path))
            }
            it.beans.addAll(mAddPosition, beans)
            if (it.beans.size > 11) {
                it.beans.removeAt(mAddPosition + beans.size)
            }
            it.notifyDataSetChanged()
        }
    }


    private fun createAddPic(): ReleaseBean {
        var bean = ReleaseBean()
        bean.isAdd = true
        bean.viewType = ITEM
        return bean
    }


    private fun createPicBean(path: String): ReleaseBean {
        var bean = ReleaseBean()
        bean.isAdd = false
        bean.path = path
        bean.viewType = ITEM
        return bean
    }

    private fun createHeader(): ReleaseBean {
        var bean = ReleaseBean()
        bean.viewType = HEADER
        return bean
    }

    private fun createFooter(): ReleaseBean {
        var bean = ReleaseBean()
        bean.viewType = FOOTER
        return bean
    }


}